/*==============================================
 * Task1c: Testing keyboard/Mouse
 *
 * Written by Dong ChangJie
 *==============================================*/

#include "GameUtils.h"
#include "GameEngine.h"
#include "Fail.h"


/** CTask 1c: Testing Game Objects.
*/
class CTask1c: public CGameEngine
{
	virtual bool Startup();
	virtual void Update(float dt);
	virtual void Draw(float dt);
	virtual void Shutdown();
protected:
	ID3DXMesh *mpMesh;
	D3DMATERIAL9 mMat;
	// TODO: you will need some extra variables here
	D3DXVECTOR3 pos;
};


bool CTask1c::Startup()
{
	if (!StartupWinD3D(640,480,false))
		return false;

	// create a teapot using the d3dx util library
	D3DXCreateTeapot(GetDevice(), &mpMesh, NULL);
	mMat=RED_MTRL;	// red material
	pos = D3DXVECTOR3(0,0,0);
	// setup default 3d options (render settings, lights, camera)
	SetupDefaultD3DOptions(GetDevice(),true);
	return true;
}
void CTask1c::Update(float dt)
{
	if (KeyPress(VK_F10))
		CloseWin();

	if (KeyPress('R'))	mMat=RED_MTRL;
	if (KeyPress('G'))	mMat=GREEN_MTRL;
	if (KeyPress('B'))	mMat=BLUE_MTRL;

	//TODO("change position using KeyDown or KeyPress");
	// or you can try GetKeyboardVector() from GameUtils.h
	// You could also try some mouse code
	D3DXVECTOR3 vel = GetKeyboardVector(WSAD_KEYS);
	float SPEED = 5.0f;
	pos += vel * dt * SPEED;
}
void CTask1c::Draw(float dt)
{
    // Clear the backbuffer to a grey color
    mpDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, WHITE_COL*0.5f, 1.0f, 0 );
	mpDevice->BeginScene();
	mpDevice->SetMaterial(&mMat);	// set material
	D3DXMATRIX world;
	// TODO("replace with some proper code");
	//D3DXMatrixIdentity(&world);	// this is wrong!
	D3DXMatrixTranslation(&world,pos.x,pos.y,pos.z);
	mpDevice->SetTransform(D3DTS_WORLD,&world);
	mpMesh->DrawSubset(0);	// draw it

	mpDevice->EndScene();
	// Present the backbuffer contents to the display
    mpDevice->Present( NULL, NULL, NULL, NULL );
}
void CTask1c::Shutdown()
{
	// SAFE_RELEASE is the same as X->Release()
	// but it checks for a NULL pointer
	SAFE_RELEASE(mpMesh);
	ShutdownD3D();
}


int WINAPI WinMain(HINSTANCE hInstance, 
				   HINSTANCE hPrevInstance, 
				   LPTSTR lpCmdLine, int nCmdShow)
{
	ENABLE_LEAK_DETECTION();

	CTask1c eng;
	return eng.Go(hInstance);	// runs everything
}
